<?php
/**
 * CreateTime: 2023/2/16 17:26
 */

namespace App\Http\Controllers\MeMediator;

use App\Consts\MeLawCaseConsts\MeLawCaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\MeLawCase\AddAcceptMediatorRequest;
use App\Http\Requests\MeMediator\AddMediatorCallbackRequest;
use App\Http\Requests\MeMediator\AddMediatorMinutesOfMeetingRequest;
use App\Models\MeLawCase\MeLawCase;
use App\Models\MeMediator\MeCallBackModel;
use App\Models\MeMediator\MeetingSummaryModel;
use Illuminate\Support\Facades\DB;

class MeMediatorController extends Controller
{
    /**
     * 调解员正式受理案件
     */
    public function acceptMediator(AddAcceptMediatorRequest $request){
        $data = $request->all();
        $data['law_case_list_ids'] = $data['law_case_list'];
        try {
            // 编辑案件正式受理时间
            (new MeLawCase())->updateByIds(explode(',',$data['law_case_list_ids']),[
                'start_mediator_time' => date("Y-m-d",time()),
                'law_case_status' => MeLawCaseConsts::ME_LAW_CASE_YSL_9,
            ]);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '调解员正式受理成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '调解员正式受理失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }


    /**
     * 新增会议记录
     */
    public function addMediatorMinutesOfMeeting(int $lawCaseId,AddMediatorMinutesOfMeetingRequest $request){
        $data = $request->all();
        $data['law_case_id'] = $lawCaseId;
        try{
            // 处理文件
            if(isset($data['files'])){
                $data['file_ids'] = implode(",",array_column($data['files'],"file_id"));
                unset($data['files']);
            }
            // 处理视频
            unset($data['video_files']);
            if(isset($data['video_files'])){
                $data['video_file_ids'] = implode(",",array_column($data['video_files'],"file_id"));
                unset($data['video_files']);
            }
            $res = (new MeetingSummaryModel())->add($data);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '添加会议纪要成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch(\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '添加会议纪要失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }

    /**
     * 编辑已盖章协议
     */
    public function sealMediator(int $lawCaseId){
        if(isset($data['seal_files'])){
            try {
                // 处理文件
                $data['seal_file_ids'] = implode(",",array_column($data['seal_files'],"file_id"));
                unset($data['seal_files']);
                (new MeLawCase())->updateData([
                    'law_case_id' =>$lawCaseId,
                    'seal_file_ids' => $data['seal_file_ids']
                ]);
                return ResponseFunc::returnJson(
                    'ok',
                    null,
                    '编辑盖章协议成功',
                    ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
                );
            }catch (\Exception $exception){
                return ResponseFunc::returnJson(
                    'error',
                    null,
                    '编辑盖章协议失败',
                    ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
                );
            }

        }else{
            return ResponseFunc::returnJson(
                'error',
                null,
                '编辑盖章协议失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }

    }

    /**
     * 回访记录
     */
    public function addMediatorCallback(int $lawCaseId,AddMediatorCallbackRequest $request){
        $data = $request->all();
        $data['law_case_id'] = $lawCaseId;
        try {
            // 处理文件
            if (isset($data['files'])) {
                $data['file_ids'] = implode(",", array_column($data['files'], "file_id"));
                unset($data['files']);
            }
            $res = (new MeCallBackModel())->add($data);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '添加跟进记录成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '添加跟进记录失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }

    /**
     * 进入诉讼
     */
    public function toLawsuit($lawCaseId){
        try {
            // 判断当前状态是否为人员调解中
            $meModel = (new MeLawCase());
            $info = $meModel->getById($lawCaseId,[
                'law_case_status'
            ]);

            if(isset($info['law_case_status'])&&$info['law_case_status']!=MeLawCaseConsts::ME_LAW_CASE_RYTJZ_4){
                return ResponseFunc::returnJson(
                    'error',
                    null,
                    '状态异常',
                    ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
                );
            }
            $res = (new MeLawCase())->updateData([
                'law_case_id' => $lawCaseId,
                'law_case_status' => MeLawCaseConsts::ME_LAW_CASE_SSZ_5
            ]);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '进入诉讼成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '进入诉讼失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }

    }

    /**
     * 结案
     */
    public function toOverClosure($lawCaseId){
        try {
            // 判断当前状态是否为人员调解中
            $meModel = (new MeLawCase());
            $info = $meModel->getById($lawCaseId,[
                'law_case_status'
            ]);
            // 非诉讼中不允许结案 TODO
//            if(isset($info['law_case_status'])&&$info['law_case_status']!=MeLawCaseConsts::ME_LAW_CASE_SSZ_5){
//                return ResponseFunc::returnJson(
//                    'error',
//                    null,
//                    '状态异常',
//                    ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
//                );
//            }

            $res = (new MeLawCase())->updateData([
                'law_case_id' => $lawCaseId,
                'law_case_status' => MeLawCaseConsts::ME_LAW_CASE_JA_7
            ]);

            return ResponseFunc::returnJson(
                'ok',
                null,
                '进入结案成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '进入结案失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }

    /**
     * 结案未回款
     * @param $lawCaseId
     * @return \Illuminate\Http\JsonResponse
     */
    public function toOverClosureNoMoney($lawCaseId){
        try {
            // 判断当前状态是否为人员调解中
            $meModel = (new MeLawCase());
            $info = $meModel->getById($lawCaseId,[
                'law_case_status'
            ]);

            if(isset($info['law_case_status'])&&$info['law_case_status']!=MeLawCaseConsts::ME_LAW_CASE_SSZ_5){
                return ResponseFunc::returnJson(
                    'error',
                    null,
                    '状态异常',
                    ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
                );
            }
            $res = (new MeLawCase())->updateData([
                'law_case_id' => $lawCaseId,
                'law_case_status' => MeLawCaseConsts::ME_LAW_CASE_JAWHK_8
            ]);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '进入结案成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '进入结案失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }



}